package com.wang.springbootinit.controller;

import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.model.param.ChatHistoryParam;
import com.wang.springbootinit.model.vo.ChatHistoryVO;
import com.wang.springbootinit.service.ChatHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.List;

/**
 * 聊天记录历史
 */
@Slf4j
@RestController
@RequestMapping("/chatHistory")
public class ChatHistoryController {
    @Resource
    private ChatHistoryService chatHistoryService;

    /**
     * 聊天记录列表
     * @param param
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<ChatHistoryVO>> list(ChatHistoryParam param, HttpServletRequest request){
        List<ChatHistoryVO> chatHistoryVOS = chatHistoryService.listByParam(param, request);
        return ResultUtils.success(chatHistoryVOS);
    }

    /**
     * 详情
     * @param param
     * @return
     */
    @PostMapping("/detail")
    public BaseResponse<List<ChatHistoryVO>> detail(@RequestBody ChatHistoryParam param, HttpServletRequest request){
        List<ChatHistoryVO> chatHistoryVOS = chatHistoryService.detail(param, request);
        return ResultUtils.success(chatHistoryVOS);
    }

    /**
     * 生成聊天记录PDF
     * @param conversationId 聊天记录参数
     * @param request HTTP请求
     */
    @GetMapping("/generatePdf")
    public void generatePdf(@RequestParam String  conversationId, HttpServletRequest request,HttpServletResponse response) {
        chatHistoryService.generatePdf(conversationId, request,response);
    }

    /**
     * 查看PDF文件
     */
    @GetMapping("/viewPdf")
    public void viewPdf(@RequestParam String filePath, HttpServletResponse response) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "文件不存在");
            }

            // 设置响应头为inline，这样浏览器会直接打开PDF
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));

            Files.copy(file.toPath(), response.getOutputStream());
            response.getOutputStream().flush();
        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "文件打开失败");
        }
    }

    /**
     * 下载PDF文件
     */
    @GetMapping("/downloadPdf")
    public void downloadPdf(@RequestParam String filePath, HttpServletResponse response) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "文件不存在");
            }

            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));

            Files.copy(file.toPath(), response.getOutputStream());
            response.getOutputStream().flush();
        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "文件下载失败");
        }
    }
}
